 package nc.vo.pubapp.pattern.model.tool;
 
 import java.util.HashSet;
import java.util.Set;

import nc.vo.pub.IAttributeMeta;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.IVOMetaStatisticInfo;
import nc.vo.pubapp.pattern.pub.PubAppTool;
 
 
 
 
 
 
 
 
 
 
 
 
 public class VOTool
 {
   public void combine(ISuperVO vo, ISuperVO anotherVO)
   {
     Set<String> set = getDifferentField(vo, anotherVO);
     Set<String> anotherSet = anotherVO.usedAttributeNames();
     for (String name : set)
     {
       if (anotherSet.contains(name))
       {
 
         Object value = anotherVO.getAttributeValue(name);
         vo.setAttributeValue(name, value);
       } }
     vo.setStatus(anotherVO.getStatus());
   }
   
 
 
 
 
 
 
   public Set<String> getDifferentField(ISuperVO vo, ISuperVO anotherVO)
   {
     Set<String> set = vo.usedAttributeNames();
     Set<String> anotherSet = anotherVO == null ? new HashSet<String>(): anotherVO.usedAttributeNames();
     
     Set<String> changedSet = new HashSet<>();
     int onlyInOld = 0;
     for (String name : set)
     {
       if (!anotherSet.contains(name)) {
         changedSet.add(name);
         onlyInOld++;
       }
       else {
         Object value = vo.getAttributeValue(name);
         Object another = anotherVO.getAttributeValue(name);
         if (!PubAppTool.isEqual(value, another)) {
           changedSet.add(name);
         }
       }
     }
     
     getAnotherSetKey(set, anotherSet, changedSet, onlyInOld);
     return changedSet;
   }
   
 
 
 
 
 
 
 
 
 
 
   private void getAnotherSetKey(Set<String> set, Set<String> anotherSet, Set<String> changedSet, int count)
   {
     int newcount = anotherSet.size() - set.size() + count;
     if (newcount == 0) {
       return;
     }
     for (String name : anotherSet) {
       if (!set.contains(name)) {
         changedSet.add(name);
         newcount--;
         if (newcount == 0) {
           break;
         }
       }
     }
   }
   
 
 
 
 
 
 
 
   public Set<String> getDifferentFieldForDynamic(ISuperVO vo, ISuperVO anotherVO)
   {
     IVOMetaStatisticInfo info = vo.getMetaData().getStatisticInfo();
     IAttributeMeta[] attributes = info.getStaticAttributes();
     Set<String> staticSet = new HashSet();
     Set<String> changedSet = new HashSet();
     
     for (IAttributeMeta attribute : attributes) {
       staticSet.add(attribute.getName());
       
       changedSet.add(attribute.getName());
     }
     
     if (vo == anotherVO) {
       return changedSet;
     }
     
     Set<String> set = vo.usedAttributeNames();
     Set<String> anotherSet = anotherVO.usedAttributeNames();
     
     int onlyInOld = 0;
     for (String name : set)
     {
       if (!staticSet.contains(name))
       {
 
 
         if (!anotherSet.contains(name)) {
           onlyInOld++;
         }
         else
         {
           Object value = vo.getAttributeValue(name);
           Object another = anotherVO.getAttributeValue(name);
           if (!PubAppTool.isEqual(value, another)) {
             changedSet.add(name);
           }
         }
       }
     }
     
 
     getAnotherSetKey(set, anotherSet, changedSet, onlyInOld);
     
     return changedSet;
   }
   
 
 
 
 
 
 
 
   public Set<String> getDifferentFieldForDynamicForClient(ISuperVO vo, ISuperVO anotherVO)
   {
     IVOMetaStatisticInfo info = vo.getMetaData().getStatisticInfo();
     IAttributeMeta[] attributes = info.getStaticAttributes();
     Set<String> staticSet = new HashSet();
     Set<String> changedSet = new HashSet();
     
     for (IAttributeMeta attribute : attributes) {
       staticSet.add(attribute.getName());
       
       changedSet.add(attribute.getName());
     }
     
     if (vo == anotherVO) {
       return changedSet;
     }
     Set<String> anotherSet = anotherVO.usedAttributeNames();
     IAttributeMeta[] allAttributes = vo.getMetaData().getAttributes();
     for (IAttributeMeta attribute : allAttributes)
     {
       String name = attribute.getName();
       if (!staticSet.contains(name))
       {
 
         if (attribute.isSerializable())
         {
 
           if ((anotherSet == null) || (anotherSet.contains(name)))
           {
 
 
             Object value = vo.getAttributeValue(name);
             Object another = anotherVO.getAttributeValue(name);
             if (!PubAppTool.isEqual(value, another))
               changedSet.add(name);
           } }
       }
     }
     return changedSet;
   }
   
 
 
 
 
 
 
   public Set<String> getDifferentFieldForPersistent(ISuperVO vo, ISuperVO anotherVO)
   {
     IVOMetaStatisticInfo info = vo.getMetaData().getStatisticInfo();
     IAttributeMeta[] attributes = info.getPerisistentAttributes();
     Set<String> persistentSet = new HashSet<>();
     for (IAttributeMeta attribute : attributes) {
       persistentSet.add(attribute.getName());
     }
     
     Set<String> changedSet = new HashSet<>();
     Set<String> set = vo.usedAttributeNames();
     Set<String> anotherSet = anotherVO == null? new HashSet<String>(): anotherVO.usedAttributeNames();
     int onlyInOld = 0;
     for (String name : set)
     {
 
       if (!anotherSet.contains(name)) {
         changedSet.add(name);
         onlyInOld++;
       }
       else {
         Object value = vo.getAttributeValue(name);
         Object another = anotherVO.getAttributeValue(name);
         if (!PubAppTool.isEqual(value, another)) {
           changedSet.add(name);
         }
       }
     }
     
 
 
     int newcount = anotherSet.size() - set.size() + onlyInOld;
     if (newcount != 0) {
       for (String name : anotherSet) {
         if ((!set.contains(name)) && (persistentSet.contains(name))) {
           changedSet.add(name);
           newcount--;
           if (newcount == 0) {
             break;
           }
         }
       }
     }
     
     return changedSet;
   }
 }

